59C - Title - CodeForces Solution


expression parsing *1600

Please click on ads to support us..

Python Code:

k=int(input())
s=list(input())
n=len(s)
for i in range(n):
    if s[i]==s[n-i-1]:
        pass
    elif s[i]=='?':
        s[i]=s[n-i-1]
    elif s[n-i-1]=='?':
        s[n-i-1]=s[i]
    else:
        print('IMPOSSIBLE')
        exit()
d=set(chr(97+i) for i in range(k) if chr(97+i) not in s)
for i in range(n//2,-1,-1):
    if s[n-i-1]==s[i]=='?':
        if len(d):
            s[n - i - 1] = s[i] = max(d)
            d.remove(max(d))
        else:
            s[n - i - 1] = s[i]='a'
if len(d):
    print('IMPOSSIBLE')
else:
    print(*s,sep='')

C++ Code:

#include <stdio.h>
#include<string.h>
#include<ctype.h>
char s[100010];
int q;
int letter[100010];
int k;

int main()
{
	scanf("%d", &k);
	scanf("%s", s);
	int ss = strlen(s);
	int t = ss / 2;
	if (ss % 2 == 1) t++;
	for (int i = 0; i < t; i++)
	{
		int j = ss - i - 1;
		if (s[i] != s[j])
		{
			if (isalpha(s[i]) && isalpha(s[j])) {
				printf("IMPOSSIBLE");
				return 0;
			}
			else if (s[i] == '?' && s[j] != '?') s[i] = s[j];
			else if (s[i] != '?' && s[j] == '?') s[j] = s[i];
		}
		else if (s[i] == '?' && s[j] == '?') q++;
		if (s[i] != '?') letter[s[i] - 'a'] = 1;
		if (s[j] != '?') letter[s[j] - 'a'] = 1;
	}
	int cnt = 0;
	for (int i = t - 1; i >= 0; i--)//从中间往前枚举
	{
		if (s[i] == '?')
		{
			int id = -1;
			for (int i = k - 1; i >= 0; i--)
			{
				if (!letter[i])//判断是否为?
				{
					id = i; break;
				}
			}
			if (id == -1) break;
			s[i] = id + 'a';
			s[ss - i - 1] = id + 'a';
			letter[id] = 1;
		}
	}
	for (int i = 0; i < k; i++)
	{
		if (!letter[i])
		{
			printf("IMPOSSIBLE");
			return 0;
		}
	}
	for (int i = 0; i < t; i++)
	{
		if (s[i] == '?')
		{
			s[i] = 'a';
			s[ss - i - 1] = 'a';
		}
	}
	printf("%s", s);
	return 0;
}
	      					 			 	   	 	    		


Comments

Submit
0 Comments
More Questions

698A - Vacations
1216B - Shooting
368B - Sereja and Suffixes
1665C - Tree Infection
1665D - GCD Guess
29A - Spit Problem
1097B - Petr and a Combination Lock
92A - Chips
1665B - Array Cloning Technique
1665A - GCD vs LCM
118D - Caesar's Legions
1598A - Computer Game
1605A - AM Deviation
1461A - String Generation
1585B - Array Eversion
1661C - Water the Trees
1459A - Red-Blue Shuffle
1661B - Getting Zero
1661A - Array Balancing
1649B - Game of Ball Passing
572A - Arrays
1455A - Strange Functions
1566B - MIN-MEX Cut
678C - Joty and Chocolate
1352E - Special Elements
1520E - Arranging The Sheep
1157E - Minimum Array
1661D - Progressions Covering
262A - Roma and Lucky Numbers
1634B - Fortune Telling